﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using webapi.Entity;

namespace webapi.Models
{
  public class SetUpCourseService
  {
    //获取开设的课程
    static public DataRowCollection getAllSetUp()
    {
      string sql = "select b.name, a.course_id, a.class_course_id, b.point, a.day, a.time, a.start, a.last, a.count from set_up_course as a join course as b on a.course_id = b.course_id";
      return DB.ExecuteSelectSql(sql).Tables[0].Rows;
    }

    //根据班级id获取课程已选人数
    static public int getSelectCountById(string id)
    {
      string sql = "select count(*) from set_up_course as a join select_course as b on a.class_course_id = b.class_course_id where a.class_course_id = '" + id + "'";
      return (int)DB.ExecuteSelectSql(sql).Tables[0].Rows[0][0];
    }

    //更新开设的课程
    static public int updateSetUpCourse(SetUpCourse setUp)
    {
      string sql = string.Format("update set_up_course set time={0}, day={1}, start={2}, last={3}, count={4} where class_course_id = '{5}'", setUp.time, setUp.day, setUp.start, setUp.last, setUp.count, setUp.class_course_id);
      return DB.ExecuteSql(sql);
    }

    //删除开设的课程
    public static int deleteSetUpCourse(string id)
    {
      string sql = "delete from set_up_course where class_course_id = '" + id + "'";
      string sql2 = "delete from select_course where class_course_id = '" + id + "'";
      DB.ExecuteSql(sql2);
      return DB.ExecuteSql(sql);
    }

    //添加开设的课程
    public static int addSetUpCourse(SetUpCourse setUp)
    {
      string sql = string.Format("insert into set_up_course(course_id, time, class_course_id, start, last, day, count) values('{0}', {1}, '{2}', {3}, {4}, {5}, {6})", setUp.course_id, setUp.time, setUp.class_course_id, setUp.start, setUp.last, setUp.day, setUp.count);
      return DB.ExecuteSql(sql);
    }

    internal static DataRowCollection searchByName(string name)
    {
      string sql = "select b.name, a.course_id, a.class_course_id, b.point, a.day, a.time, a.start, a.last, a.count from set_up_course as a join course as b on a.course_id = b.course_id where b.name like '%" + name + "%'";
      return DB.ExecuteSelectSql(sql).Tables[0].Rows;
    }
  }
}